import fs from 'node:fs'
import { Version } from './components/index.js'

const files = fs.readdirSync('./plugins/impart-pro-plugin/apps').filter(file => file.endsWith('.js'))

let ret = []

logger.info('---------=.=---------')
logger.info(`impart插件${Version.version}载入成功`)
logger.info(`作者-sumght 2023.5.5`)
if (Math.floor(Math.random() * 50) > 30) {
  logger.info(`██╗███╗   ███╗██████╗  █████╗ ██████╗ ████████╗`)
  logger.info(`██║████╗ ████║██╔══██╗██╔══██╗██╔══██╗╚══██╔══╝`)
  logger.info(`██║██╔████╔██║██████╔╝███████║██████╔╝   ██║`)
  logger.info(`██║██║╚██╔╝██║██╔═══╝ ██╔══██║██╔══██╗   ██║`)
  logger.info(`██║██║ ╚═╝ ██║██║     ██║  ██║██║  ██║   ██║`)
  logger.info(`╚═╝╚═╝     ╚═╝╚═╝     ╚═╝  ╚═╝╚═╝  ╚═╝   ╚═╝`)
}

files.forEach((file) => {
  ret.push(import(`./apps/${file}`))
})

if (!global.segment) {
  try {
    global.segment = (await import('oicq')).segment
  } catch (err) {
    global.segment = (await import('icqq')).segment
  }
}

ret = await Promise.allSettled(ret)

let apps = {}
for (let i in files) {
  let name = files[i].replace('.js', '')

  if (ret[i].status != 'fulfilled') {
    logger.error(`载入插件错误：${logger.red(name)}`)
    logger.error(ret[i].reason)
    continue
  }
  apps[name] = ret[i].value[Object.keys(ret[i].value)[0]]
}

export { apps }